#define F_CPU 16000000
#define BAUD 9600
#define MYUBRR F_CPU/16/BAUD-1
#include <avr/io.h>
#include <util/delay.h>
#include <inttypes.h>

void USART_Init(uint8_t ubrr){
	/*Set baud rate */
	UBRR0H = (uint8_t)(ubrr>>8);
	UBRR0L = (uint8_t)ubrr;
	/*Enable receiver and transmitter */
	UCSR0B = (1<<TXEN0);
	/* Set frame format: 8data, 1stop bit */
	UCSR0C = (1<<UCSZ01)|(3<<UCSZ00);
}
void USART_Transmit( uint8_t data ){
	/* Wait for empty transmit buffer */
	while ( !( UCSR0A & (1<<UDRE0)) );
	/* Put data into buffer, sends the data */
	UDR0 = data;
}

int main(void)
{
	uint8_t temp;
	DDRC = 0;		//set analog pins to input
	DDRB = 0xFF;	//port b to output
	DDRD = 0xFF;	//port d to output
	
	//UBRR0H = (MYUBRR>>8);
	//UBRR0L = MYUBRR;
	
	ADMUX = _BV(REFS0); //Vref=Vavcc, right justified, ADC0 single-end select
	ADCSRA = _BV(ADEN) | _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0); //start ADC with 128bit prescale for 125kHz
	USART_Init(MYUBRR);
	while(1){
		ADCSRA |= (1<<ADSC);	//start conversion
		while((ADCSRA&(1<<ADIF))==0); //wait for end of conversion
		PORTD = ADCL;	//give the low byte to PORTD
		PORTB = ADCH;
		temp = ADC;
		//temp = temp;
		USART_Transmit(temp*5);
		_delay_ms(1000);
	}
	return 0;
}